package com.panopset.java;

import java.util.ArrayList;
import java.util.List;

import com.panopset.compat.Strings;

/**
 * Format a String list.
 * @author Karl Dinwiddie
 *
 */
public class FormatList {

    /**
     * Source list.
     */
    private final List<String> srcList;

    /**
     * @param s String array.
     */
    public FormatList(final String... s) {
        List<String> l = new ArrayList<String>();
        for (String t : s) {
            l.add(t);
        }
        srcList = l;
    }
    /**
     * @param sourceList Source list.
     */
    public FormatList(final List<String> sourceList) {
        this.srcList = sourceList;
    }

    /**
     *
     * @return Source list.
     */
    public final List<String> getSourceList() {
        return srcList;
    }

    /**
     * Search string, split on two lines to ensure that it is not replaced when
     * FormatSource changes the real one.
     */
    private static final String APP_VERSION_ASSIGNMENT = "public static "
            + "final long UNI_VERSION =";

    /**
     * Pre-compile time replacement line.
     */
    private static final String REPLACEMENT_LINE = "    "
            + APP_VERSION_ASSIGNMENT + " "
            + Version.getInstance().createNewVersion() + "L;";

    /**
     * Source.
     */
    private String src;

    /**
     * @return source.
     */
    public final String getSource() {
        if (src == null) {
            src = Strings.list2str(getSourceList());
        }
        return src;
    }

    /**
     * Result list.
     */
    private List<String> resultList;

    /**
     * @return Formatted String.
     */
    public final List<String> getResultList() {
        if (resultList == null) {
            resultList = new ArrayList<String>();
            int i = 0;
            int lastPopulated = -1;
            for (String s : getSourceList()) {
                if (!s.trim().equals("")) {
                    lastPopulated = i;
                }
                i++;
            }
            i = 0;
            FOR: for (String s : getSourceList()) {
                if (i <= lastPopulated) {
                    if (s.indexOf(APP_VERSION_ASSIGNMENT) > -1) {
                        s = REPLACEMENT_LINE;
                    }
                    resultList.add(trimEnd(s).replace("\u0009", "    ").replace(
                            "{\u0020", "{").replace("{" + "}", "{\u0020}"));
                } else {
                    break FOR;
                }
                i++;
            }
            resultList.add("");
        }
        return resultList;
    }


    /**
     *
     */
    private String result;

    /**
     * Format a vector of Java source lines.
     *
     * @return Formatted Java source.
     */
    public final String getResult() {
        if (result == null) {
            result = Strings.list2str(getResultList());
        }
        return result;
    }

    /**
     *
     * Trim the spaces off the end of a String.

     * <h5>References</h5>
     * <ul>
     * <li>
     * <a
     * href="http://java-interview-faqs.blogspot.com/2009/07/playing-with-java-
     * string-trim-basics.html
     * ">java-interview-faqs.blogspot.com/2009/07/playing-with-java-
     * string-trim-basics.html</a>.
     * </li></ul>
     *
     * @param str
     *            String.
     * @return String s without any trailing spaces.
     */
    public static String trimEnd(final String str) {
        String s = str;
        if (s == null) {
            return "";
        }
        while (s.endsWith(" ")) {
            s = s.substring(0, s.length() - 1);
        }
        return s;
    }

}
